//
//  ALPS_RadioController.m
//  Alps
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_RadioController.h"
#import "ALPS_TRadioTCell.h"
#import "ALPS_TRadioModel.h"
#import "ALPS_TRadioTopView.h"
#import "ALPS_TRadioTopCCCell.h"
#import "ALPS_TSortingRadioViewController.h"
#import "ALPS_TRadioListViewController.h"
#import "ALPS_TCWRadioViewController.h"
#import "ALPS_TProvinceViewController.h"
#import "ALPS_TLocalRadioViewController.h"
@implementation ALPS_RadioController
- (void)dealloc
{
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataSource = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    self.topArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithWhite:0.908 alpha:1.000];
    [self getView];
    [self getData];
    [self defaultCenterPushCollectVC];
}

//界面
-(void)getView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64 - 35) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[ALPS_TRadioTCell class] forCellReuseIdentifier:@"TRadioTCell"];
    
    
}

//数据请求
-(void)getData
{
    [WSSNetTool GetNetByURL:@"http://live.ximalaya.com/live-web/v4/homepage?device=iPhone" body:nil headers:nil response:0 success:^(id result) {
        //第一个cell
        NSDictionary *dic  = [result objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"localRadios"];
        for (NSDictionary *dic in arr) {
            ALPS_TRadioModel *model = [[ALPS_TRadioModel alloc] initWithDic:dic];
            [self.dataSource addObject:model];
        }
        //第二个排行榜
        NSArray *array = [dic objectForKey:@"topRadios"];
        for (NSDictionary *dict in array) {
            ALPS_TRadioModel *model = [[ALPS_TRadioModel alloc] initWithDic:dict];
            [self.dataArray addObject:model];
        }
        
        //头视图请求数据
        NSArray *topArray = [dic objectForKey:@"categories"];
        for (NSDictionary *dicti in topArray) {
            ALPS_TRadioModel *model = [[ALPS_TRadioModel alloc] initWithDic:dicti];
            [self.topArray addObject:model];
        }
        if (self.topArray.count > 0) {
            //创建头视图
            ALPS_TRadioTopView *view = [[ALPS_TRadioTopView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, 230)];
            view.dataSoucre = self.topArray;
            self.tableView.tableHeaderView = view;
            //跳转各类电台
            view.value = ^(NSInteger num, NSString *string)
            {
                if (!(num < 0)) {
                    ALPS_TSortingRadioViewController *srVC = [[ALPS_TSortingRadioViewController alloc] init];
                    srVC.value = num;
                    srVC.navigationItem.title = string;
                    [self.navigationController pushViewController:srVC animated:YES];
                }
                
            };
            //跳转国家台 网络台
           view.auxiliaryValue = ^(NSInteger temp)
            {
                if (temp == 1 || temp == 3) {
                    ALPS_TCWRadioViewController *cwrVC = [[ALPS_TCWRadioViewController alloc] init];
                    cwrVC.type = temp;
                    if (temp == 1) {
                        cwrVC.navigationItem.title = @"国家台";
                    }
                    else
                    {
                        cwrVC.navigationItem.title = @"网络台";
                    }
                    
                    [self.navigationController pushViewController:cwrVC animated:YES];
                }
                //跳转省市台
                if (temp == 2) {
                    ALPS_TProvinceViewController *pVC = [[ALPS_TProvinceViewController alloc] init];
                    pVC.navigationItem.title = @"省市台";
                    [self.navigationController pushViewController:pVC animated:YES];
                }
                if (temp == 0) {
                    ALPS_TLocalRadioViewController *lrVc = [[ALPS_TLocalRadioViewController alloc] init];
                    lrVc.navigationItem.title = @"本地台";
                    [self.navigationController pushViewController:lrVc animated:YES];
                    
                }
                
            };
        }
        
        if (self.dataSource.count > 0 && self.dataArray.count > 0) {
            
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 350;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
       //进入推荐页
        ALPS_TRadioTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TRadioTCell"];
        cell.dataSource = self.dataSource;
        cell.selectionStyle = UITableViewCellStyleDefault;
        //推荐里的三个cell跳转
        cell.radioValue = ^(NSInteger num)
        {
            if (num >= 0) {
                
                ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc]init];
                ALPS_TRadioModel *model = [self.dataSource objectAtIndex:num];
                NSDictionary *dic = model.playUrl;
                pmVC.radioName = model.name;
                pmVC.radioURL = [dic objectForKey:@"aac64"];
                pmVC.isRedioYesOrNo = YES;
                pmVC.pictureURL = model.coverLarge;
                [self.navigationController pushViewController:pmVC animated:YES];
            }
        
        };
        return cell;
    }
    //进入排行榜
    ALPS_TRadioTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TRadioTCell"];
    cell.titleLabel.text = @"排行榜";
    cell.selectionStyle = UITableViewCellStyleDefault;
    cell.dataSource = self.dataArray;
    //排行榜里的三个cell跳转
    cell.radioValue = ^(NSInteger num)
    {
        if (num >= 0) {
            ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc]init];
            ALPS_TRadioModel *model = [self.dataArray objectAtIndex:num];
            NSDictionary *dic = model.playUrl;
            pmVC.radioName = model.name;
            pmVC.radioURL = [dic objectForKey:@"aac64"];
            pmVC.isRedioYesOrNo = YES;
            pmVC.pictureURL = model.coverLarge;
            [self.navigationController pushViewController:pmVC animated:YES];
        }
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ALPS_TSortingRadioViewController *TsrVC = [[ALPS_TSortingRadioViewController alloc] init];
        TsrVC.type = 2;
        TsrVC.navigationItem.title = @"推荐";
        [self.navigationController pushViewController:TsrVC animated:YES];
    }
    else
    {
    ALPS_TRadioListViewController *tlVC = [[ALPS_TRadioListViewController alloc] init];
        tlVC.navigationItem.title = @"排行榜";
    [self.navigationController pushViewController:tlVC animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
